package com.example.zy.vote1.controller;

import com.example.zy.vote1.bean.User;
import com.example.zy.vote1.services.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/view")
public class ViewController {
    @Autowired
    private UserServices userServicesImpl;
    private static final org.slf4j.Logger log=org.slf4j.LoggerFactory.getLogger(ViewController.class);
    @ResponseBody
    @RequestMapping("/login")
    public ModelAndView login()
    {
        log.info("视图跳转。。。");
        return new ModelAndView("login");
    }

    @ResponseBody
    @RequestMapping("/register")
    public ModelAndView register()
    {
        log.info("视图跳转。。。");
        return new ModelAndView("register");
    }
    @ResponseBody
    @RequestMapping("/v_login")
    public ModelAndView v_login()
    {
        log.info("视图跳转。。。");
        return new ModelAndView("v_login");
    }
    @ResponseBody
    @RequestMapping("/v_users_table_test")
    public ModelAndView v_users_table_test()
    {
        log.info("视图跳转。。。");
        return new ModelAndView("v_users_table_test");
    }
    @ResponseBody
    @RequestMapping("/v_upload")
    public ModelAndView v_upload()
    {
        log.info("视图跳转。。。");
        return new ModelAndView("v_upload");
    }
    @ResponseBody
    @RequestMapping("/getUserListByPage")
    public Map getUserListByPage(HttpServletRequest request)
    {
        //当前页
        String page = request.getParameter("page");
        //每页记录数
        String limit = request.getParameter("limit");
        int limitP = Integer.valueOf(limit);
//        List<User> userListByPage = userServicesImpl.getUserListByPage(Integer.valueOf(page) - 1, Integer.valueOf(limit));
        int pageP =  (Integer.valueOf(page) - 1)*limitP;

        List<User> userList = userServicesImpl.getUserListByPage(pageP,limitP);
        //TODO 查询记录数
        int count = (int)(Math.random()*1000);
        //TODO userlist查询
        Map pageInfo = new HashMap();
        pageInfo.put("code",0);
        pageInfo.put("msg","");
        //TODO count数量查询
        pageInfo.put("count",count);
        pageInfo.put("data",userList);
        return pageInfo;
    }
    @RequestMapping(value = "/check_login",method = RequestMethod.POST)
    public ModelAndView login(User user,HttpServletRequest request){
        System.out.println(user.getName()+","+user.getPwd());
        User fUser = userServicesImpl.login(user);
        ModelAndView mv = new ModelAndView();
        if(null != fUser && fUser.getId() != null){
            mv.setViewName("v_background");
            mv.addObject("user",fUser);
            request.getSession().setAttribute("sUser",fUser);
        }else{
            log.info("视图跳转。。。");
            mv.setViewName("fail");
            mv.addObject("msg","账号密码不正确");
        }
        return mv;
    }
}
